NumPy Indexing and Selection
In this lecture we will discuss how to select elements or groups of elements from an array.
import numpy as np |
Bracket Indexing and Selection
The simplest way to pick one or some elements of an array looks very similar to python lists:
#Get values in a range |
Broadcasting
Numpy arrays differ from a normal Python list because of their ability to broadcast:
#Setting a value with index range (Broadcasting) |
Now note the changes also occur in our original array!
arr |
Data is not copied, it’s a view of the original array! This avoids memory problems!
To copy data:
#To get a copy, need to be explicit |
Indexing a 2D array (matrices)
The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity.
arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45])) |
Fancy Indexing
Fancy indexing allows you to select entire rows or columns out of order,to show this, let’s quickly build out a numpy array:
#Set up array |
Fancy indexing allows the following
arr2d[[2,4,6,8]] |
Selection
Let’s briefly go over how to use brackets for selection based off of comparison operators.
arr = np.arange(1,11)
arr
>> array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr > 4
>> array([False, False, False, False, True, True, True, True, True, True], dtype=bool)
bool_arr = arr>4
bool_arr
>> array([False, False, False, False, True, True, True, True, True, True], dtype=bool)
arr[bool_arr]
>> array([ 5, 6, 7, 8, 9, 10])
arr[arr>2]
>> array([ 3, 4, 5, 6, 7, 8, 9, 10])
x = 2
arr[arr>x]
>> array([ 3, 4, 5, 6, 7, 8, 9, 10])